<?php
namespace Muyuxuan\ThreePayment\three\bct\tool\Demo;
use Muyuxuan\ThreePayment\three\bct\tool\SecurityUtil\NewRsaUtil;
use Muyuxuan\ThreePayment\three\bct\tool\Util\HttpClient;
use Muyuxuan\ThreePayment\three\bct\tool\Util\Log;
use Muyuxuan\ThreePayment\three\bct\tool\Util\Tools;

/**
 * @Desc:绑定关系查询接口
 * @author dasheng@baofu.com(大圣)
 * @datetime 2025/4/7 16:20
 */

require_once "../Init/init.php";


$ServiceTp = "T-1001-013-22";

$HeaderPost = array();
$HeaderPost["memberId"] = $GLOBALS["Member_Id"];//宝付提供给商户的唯一编号
$HeaderPost["terminalId"] = $GLOBALS["Terminal_Id"];
$HeaderPost["serviceTp"] = $ServiceTp;//报文编号
$HeaderPost["verifyType"] = "1";//加密方式目前只有1种，请填：1

$DataBody = [];
$DataBody["version"] = "4.1.0";
$DataBody["contractNo"] = "CM610000000000185348";
$DataBody["requestNo"] = Tools::getTsnOderid("RN");

$contentData = array();
$contentData["header"] = $HeaderPost;
$contentData["body"] = $DataBody;

$ContentStr = json_encode($contentData);
Log::EchoFormat("JSON序列号：".$ContentStr);
$enContent = NewRsaUtil::encryptByPfx($ContentStr,$GLOBALS["PFX_FILE_PATH"],$GLOBALS["Key_Pwd"]);

$HeaderPost["content"] = $enContent;


$ResultPost  = HttpClient::Post($HeaderPost,Tools::getUrl($HeaderPost["serviceTp"]));
Log::EchoFormat("返回参数：".$ResultPost);
if(empty($ResultPost)) throw new \Exception("请求返回异常");

if(strpos($ResultPost,"header") != false)
{
    $resulterror = json_decode($ResultPost,true);
    var_dump($resulterror);
}else{
    $DePostString = NewRsaUtil::decrptByPub($ResultPost,$GLOBALS["CER_FILE_PATH"]);
    Log::EchoFormat("解密返回：".$DePostString);
    if(empty($DePostString))  throw new \Exception("解密空值，请检查证书是否正常");
    if(strpos($DePostString,"header"))
    {
        $contenBe = json_decode($DePostString,true);
        if($contenBe["header"]["sysRespCode"] === "S_0000")
        {
            Log::EchoFormat("业务层结果:".json_encode($contenBe["body"]));
            if($contenBe["body"]["retCode"] === 1){
                Log::EchoFormat("绑定关系成功:".json_encode($contenBe["body"]));
            }else{
                Log::EchoFormat("查询失败:".json_encode($contenBe["body"],JSON_UNESCAPED_UNICODE));
            }
        }
    }
}
